package com.example.demo.controller;

import com.example.demo.LockUtil;
import com.example.demo.pojo.User;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.Comparator;
import java.util.TreeSet;
import java.util.concurrent.TimeUnit;

@RestController
public class OrderController {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @GetMapping("/orders/submitOrder/{productId}")
    public String submit(@PathVariable String productId) {
        /*TreeSet<User> treeSet = new TreeSet<User>(new Comparator<User>() {
            @Override
            public int compare(User o1, User o2) {
                return o1.compareTo(o2);
               *//* if(o1.getId()>o2.getId()) return 1;
                if(o1.getId()==o2.getId()) return 0;
                return -1;*//*
//               return o1.getId().compareTo(o2.getId());
            }
        });

        RedissonClient redissonClient = null;
        redissonClient.getRedLock(a,b,c);
        redissonClient.getLock(productId).lock(30,TimeUnit.MINUTES);
        boolean lock = new LockUtil().lock(productId);
        try {
            if(!lock) {
                return "error";
            }
            int stock = Integer.parseInt(stringRedisTemplate.opsForValue().get("stock"));
            if (stock > 0) {
                // service a 10
                // service b 30
                // service c
                // service a -->service b---service c
                stock--;

                stringRedisTemplate.opsForValue().set("stock", "" + stock);
                System.out.println("购买成功，库存：" + stock);
            } else {
                System.out.println("库存不足");
            }
        } finally {
            stringRedisTemplate.delete(productId);
        }*/
        return "sucess";

    }
}
